package com.quectel.emp.controller.household;


import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.household.dto.HouseholdDto;
import com.quectel.core.module.household.dto.HouseholdSocialRelationsDto;
import com.quectel.core.module.household.service.HouseholdService;
import com.quectel.core.module.household.service.HouseholdSocialRelationsService;
import com.quectel.emp.controller.BaseController;
import com.quectel.emp.util.SessionHolder;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/03/10 11:46
 */
@RestController
@RequestMapping("householdSocialRelations")
@Tag(name = "HouseholdSocialRelationsController", description = "住户社会关系相关api")
public class HouseholdSocialRelationsController extends BaseController {

    @DubboReference
    private HouseholdService householdService;
    @DubboReference
    private HouseholdSocialRelationsService householdSocialRelationsService;


    /**
     * 查询单条住户社会关系记录，编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @Operation(summary = "根据id查询单条住户社会关系记录，编辑查询反显")
    public Response<HouseholdSocialRelationsDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        params.put(LambdaUtil.getFieldName(HouseholdSocialRelationsDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdSocialRelationsDto::getVillageId), SessionHolder.getEmp().getVillageId());
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (householdSocialRelationsService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        HouseholdSocialRelationsDto householdSocialRelationsDto = householdSocialRelationsService.selectById(id);

        return Response.<HouseholdSocialRelationsDto>ok().wrap(householdSocialRelationsDto);
    }

    /**
     * 根据id查询单条住户社会关系记录
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @Operation(summary = "根据id查询单条住户社会关系记录")
    public Response<HouseholdSocialRelationsDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        params.put(LambdaUtil.getFieldName(HouseholdSocialRelationsDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdSocialRelationsDto::getVillageId), SessionHolder.getEmp().getVillageId());
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (householdSocialRelationsService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        HouseholdSocialRelationsDto householdSocialRelationsDto = householdSocialRelationsService.selectById(id);

        return Response.<HouseholdSocialRelationsDto>ok().wrap(householdSocialRelationsDto);
    }

    /**
     * 保存住户社会关系单条记录
     *
     * @param householdSocialRelationsDto
     * @return
     */
    @PostMapping("save")
    @Operation(summary = "保存住户社会关系单条记录")
    public Response<Object> save(@RequestBody @Validated HouseholdSocialRelationsDto householdSocialRelationsDto) {

        Map<String, Object> params = new HashMap<>();
        //添加租户和小区筛选
        params.put(LambdaUtil.getFieldName(HouseholdDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdDto::getVillageId), SessionHolder.getEmp().getVillageId());
        params.put(LambdaUtil.getFieldName(HouseholdDto::getId), householdSocialRelationsDto.getHouseholdId());
        if (householdService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        householdSocialRelationsDto.setTenantId(SessionHolder.getEmp().getTenantId());
        householdSocialRelationsDto.setVillageId(SessionHolder.getEmp().getVillageId());

        householdSocialRelationsService.save(householdSocialRelationsDto);

        return Response.ok();
    }


    /**
     * 修改社会关系单条记录
     *
     * @param householdSocialRelationsDto
     * @return
     */
    @PostMapping("update")
    @Operation(summary = "修改社会关系单条记录")
    public Response<Object> update(@RequestBody @Validated HouseholdSocialRelationsDto householdSocialRelationsDto) {

        Map<String, Object> params = new HashMap<>();
        params.put(LambdaUtil.getFieldName(HouseholdSocialRelationsDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdSocialRelationsDto::getVillageId), SessionHolder.getEmp().getVillageId());
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(householdSocialRelationsDto.getId()));
        if (householdSocialRelationsService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        householdSocialRelationsDto.setTenantId(SessionHolder.getEmp().getTenantId());
        householdSocialRelationsDto.setVillageId(SessionHolder.getEmp().getVillageId());

        householdSocialRelationsService.updateById(householdSocialRelationsDto);

        return Response.ok();
    }

    /**
     * 批量删除社会关系单条记录
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @Operation(summary = "批量删除社会关系单条记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        params.put(LambdaUtil.getFieldName(HouseholdSocialRelationsDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdSocialRelationsDto::getVillageId), SessionHolder.getEmp().getVillageId());

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (householdSocialRelationsService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        householdSocialRelationsService.deleteBatch(ids);

        return Response.ok();
    }
}
